package net.hn.hnms.biz.safety.trinity.service;

import lombok.RequiredArgsConstructor;
import net.hn.hnms.biz.api.feign.model.DataStateVo;
import net.hn.hnms.biz.common.jpa.utils.ResponsePage;
import net.hn.hnms.biz.safety.trinity.dao.HiddenDangerAcceptDao;
import net.hn.hnms.biz.safety.trinity.dao.HiddenDangerListDao;
import net.hn.hnms.biz.safety.trinity.dao.HiddenDangerReformDao;
import net.hn.hnms.biz.safety.trinity.entity.HiddenDangerList;
import net.hn.hnms.biz.safety.trinity.until.Dictionaries;
import net.hn.hnms.sys.common.mybatis.core.page.PageQuery;
import net.hn.hnms.biz.api.safety.trinity.model.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import net.hn.hnms.biz.safety.trinity.entity.HiddenDangerRecordVo;
import net.hn.hnms.biz.safety.trinity.entity.HiddenDangerRecordDto;

import javax.persistence.EntityManager;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author 智呈物联 nzw
 * @date 2022/8/4
 */
@Service
@RequiredArgsConstructor
public class HiddenDangerRecordService {

    private final JdbcTemplate locationJdbcTemplate;

    HiddenDangerListDao hiddenDangerListDao;

    HiddenDangerAcceptDao hiddenDangerAcceptDao;

    HiddenDangerReformDao hiddenDangerReformDao;

    EntityManager entityManager;

    @Autowired
    public void setHiddenDangerListDao(HiddenDangerListDao hiddenDangerListDao) {
        this.hiddenDangerListDao = hiddenDangerListDao;
    }

    @Autowired
    public void setHiddenDangerAcceptDao(HiddenDangerAcceptDao hiddenDangerAcceptDao) {
        this.hiddenDangerAcceptDao = hiddenDangerAcceptDao;
    }

    @Autowired
    public void setHiddenDangerReformDao(HiddenDangerReformDao hiddenDangerReformDao) {
        this.hiddenDangerReformDao = hiddenDangerReformDao;
    }

    @Autowired
    public void setEntityManager(EntityManager entityManager) {
        this.entityManager = entityManager;
    }

    private String getDate(){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        Calendar calendarM = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR,calendarM.get(Calendar.YEAR));
        Date time = calendar.getTime();
        return simpleDateFormat.format(time);
    }

    public DangerInfoVo hiddenDanger(String mineCode){
        Dictionaries.setMineCode(mineCode);
        DangerInfoVo dangerInfoVo = new DangerInfoVo();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String dataTime = simpleDateFormat.format(new Date());
        dataTime+=" 00:00:00";
        Timestamp timestamp = Timestamp.valueOf(dataTime);
        //新增隐患
        List<HiddenDangerList> hiddenDangerLists = hiddenDangerListDao.findAllLikeDataTime(timestamp,mineCode);
        dangerInfoVo.setAdded(hiddenDangerLists.size());
        //累计验收通过的隐患
        String time = getDate();
        Integer hiddenDangerAccepts = hiddenDangerListDao.ClosedLoopCount(time,mineCode,Dictionaries.DANGER_STATE_CLOSEDLOOP);
        dangerInfoVo.setClosedLoopCount(hiddenDangerAccepts);
        //整改隐患
        Integer hiddenDangerReforms = hiddenDangerListDao.NotClosedLoopCount(time,mineCode,Dictionaries.DANGER_STATE_NOTCLOSEDLOOP);
        dangerInfoVo.setNotClosedLoopCount(hiddenDangerReforms);

        return dangerInfoVo;
    }

    public DangerInfoVo gaoTouYaoHiddenDanger(String mineCode){
        Dictionaries.setMineCode(mineCode);
        DangerInfoVo dangerInfoVo = new DangerInfoVo();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String dataTime = simpleDateFormat.format(new Date());
        dataTime+=" 00:00:00";
        Timestamp timestamp = Timestamp.valueOf(dataTime);
        //新增隐患
        List<HiddenDangerList> hiddenDangerLists = hiddenDangerListDao.findAllLikeDataTime(timestamp,mineCode);
        dangerInfoVo.setAdded(hiddenDangerLists.size());
        //累计验收通过的隐患
        String time = getDate();
        Integer hiddenDangerAccepts = hiddenDangerListDao.gaoTouYaoClosedLoopCount(mineCode,Dictionaries.DANGER_STATE_CLOSEDLOOP);
        dangerInfoVo.setClosedLoopCount(hiddenDangerAccepts);
        //整改隐患
        Integer hiddenDangerReforms = hiddenDangerListDao.NotGaoTouYaoClosedLoopCount(time,mineCode,Dictionaries.DANGER_STATE_NOTCLOSEDLOOP);
        dangerInfoVo.setNotClosedLoopCount(hiddenDangerReforms);

        return dangerInfoVo;
    }

    public HiddenDangerCheckVo hiddenDangerCheck(String mineCode){

        Dictionaries.setMineCode(mineCode);
        String time = getDate();
        HiddenDangerCheckVo hiddenDangerCheckVo = new HiddenDangerCheckVo();
        //隐患总数
        int dangerCount = hiddenDangerListDao.countAll(time,mineCode);
        hiddenDangerCheckVo.setDangerCount(dangerCount);
        //重大隐患
        hiddenDangerCheckVo.setMajor(hiddenDangerListDao.countAllLevel(time,mineCode,Dictionaries.DANGER_LEVEL_MANJOR));

        //待整改隐患
        int rectified = hiddenDangerListDao.rectified(time,mineCode);
        hiddenDangerCheckVo.setRectified(rectified);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Timestamp timestamp = Timestamp.valueOf(simpleDateFormat.format(new Date()));

        //所有超过期限的隐患
        int dealine = hiddenDangerListDao.countAllDeadline(time,mineCode);
        //已经验收的隐患总数
        int taccept = hiddenDangerListDao.ClosedLoopCount(time,mineCode,Dictionaries.DANGER_STATE_CLOSEDLOOP);
        hiddenDangerCheckVo.setOverdue(dealine);

        return hiddenDangerCheckVo;
    }

    public HiddenDangerCheckVo gaotouyaoHiddenDangerCheck(String mineCode){

        Dictionaries.setMineCode(mineCode);
        HiddenDangerCheckVo hiddenDangerCheckVo = new HiddenDangerCheckVo();
        //隐患总数
        int dangerCount = hiddenDangerListDao.gaoTouYaoCountAll(mineCode);
        hiddenDangerCheckVo.setDangerCount(dangerCount);
        //重大隐患
        hiddenDangerCheckVo.setMajor(hiddenDangerListDao.gaoTouYaoCountAllLevel(mineCode,Dictionaries.DANGER_LEVEL_MANJOR));

        //待整改隐患
        int rectified = hiddenDangerListDao.notRectified(mineCode);
        hiddenDangerCheckVo.setRectified(rectified);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

        //所有超过期限的隐患
        int dealine = hiddenDangerListDao.gaoTouYaoCountAllDeadline(simpleDateFormat.format(new Date()),mineCode);
        hiddenDangerCheckVo.setOverdue(dealine);

        return hiddenDangerCheckVo;
    }

    public ResponsePage<HiddenDangerRecordVo> page(PageQuery pageQuery, HiddenDangerRecordDto hiddenDangerRecordDto){

        HiddenDangerList hiddenDangerList = new HiddenDangerList();
        hiddenDangerList.setDangerMajor(hiddenDangerRecordDto.getMajor());
        hiddenDangerList.setDangerFrom(hiddenDangerRecordDto.getSource());
        hiddenDangerList.setDangerLevel(hiddenDangerRecordDto.getDangerLevel());
        String dangerLevel = hiddenDangerRecordDto.getDangerLevel();
        String dangerState = hiddenDangerRecordDto.getDangerState();
        String mineCode = hiddenDangerRecordDto.getMineCode();
        String startDay = hiddenDangerRecordDto.getStartDay();
        if (startDay==null){
            startDay = "";
        }
        Dictionaries.setMineCode(mineCode);
        String endDay = hiddenDangerRecordDto.getEndDay();
        if (endDay == null){
            endDay = "";
        }

        String sql = "select * from hidden_danger_list where 1=1";
        String countSql = "select count(*) from hidden_danger_list where 1=1";
        String where = "";
        if(mineCode=="150621011038") {
            where += "and (( danger_status = '1' AND approve_status = '1' ) \n" +
                    "\tOR ( danger_status = '0' AND approve_status = '1' ) \n" +
                    "\tOR ( danger_status = '3' AND approve_status = '2' ))";
        }
        if (!"".equals(startDay)){
            where += " and check_date>= '"+startDay+"'" ;
        }
        if (!"".equals(endDay)){
            where += " and check_date <= '"+endDay+"'";
        }
        if (dangerLevel!=null&&!"".equals(dangerLevel)){
            where+="and danger_level = '"+dangerLevel+"' ";
        }
        if (dangerState!=null&&!"".equals(dangerState)){
            where+="and danger_status = '"+dangerState+"' ";
        }
        where +=" and trim(mine_code) = '"+mineCode+"'";

        Integer pageSize = pageQuery.getPageSize();
        if (pageSize==null){
            pageSize = 10;
        }
        Integer pageNum = pageQuery.getPageNum();
        if (pageNum==null||pageNum==0){
            pageNum=1;
        }

        String page = " order by check_date desc LIMIT "+pageSize+" OFFSET "+(pageNum-1)*pageSize;

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        List<HiddenDangerList> lists= locationJdbcTemplate.query(sql+where+page, BeanPropertyRowMapper.newInstance(HiddenDangerList.class));
        Map<String,Object> map = locationJdbcTemplate.queryForMap(countSql+where);
        List<HiddenDangerRecordVo> hiddenDangerRecordVoList = new ArrayList<>();
        for (HiddenDangerList hiddenDangerList1 : lists){
            HiddenDangerRecordVo hiddenDangerRecordVo = new HiddenDangerRecordVo();
            //隐患地点
            hiddenDangerRecordVo.setDangerLocation(hiddenDangerList1.getDangerLocation());
            //检查时间
            if (hiddenDangerList1.getCheckDate()!=null){
                hiddenDangerRecordVo.setCheckDate(simpleDateFormat.format(hiddenDangerList1.getCheckDate()));
            }
            //检查人
            hiddenDangerRecordVo.setCheckPersonName(hiddenDangerList1.getCheckPersonName());
            //隐患级别
            hiddenDangerRecordVo.setDangerLevel(getLevelName(hiddenDangerList1.getDangerLevel()));
            //隐患专业
            hiddenDangerRecordVo.setDangerMajor(hiddenDangerList1.getDangerMajor());
            //隐患描述
            hiddenDangerRecordVo.setDangerDescription(hiddenDangerList1.getDangerDescription());
            //整改人
            hiddenDangerRecordVo.setReformPersonName(hiddenDangerList1.getReformPersonName());
            //整改措施
            hiddenDangerRecordVo.setReformPolicy(hiddenDangerList1.getReformPolicy());
            //整改期限
            if (hiddenDangerList1.getReformDeadline()!=null){
                hiddenDangerRecordVo.setReformDeadline(simpleDateFormat.format(hiddenDangerList1.getReformDeadline()));
            }
            //验收人
            hiddenDangerRecordVo.setAcceptPersonNames(hiddenDangerList1.getAcceptPersonNames());
            //验收时间
            if (hiddenDangerList1.getAcceptDate()!=null) {
                hiddenDangerRecordVo.setAcceptDate(simpleDateFormat.format(hiddenDangerList1.getAcceptDate()));
            }
            //隐患状态
            if (Dictionaries.GAOTOUYAOMINECODE.equals(mineCode)){
                hiddenDangerRecordVo.setDangerStatus(getStatusName(hiddenDangerList1.getDangerStatus()));
            }else {
                hiddenDangerRecordVo.setDangerStatus(hiddenDangerList1.getDangerStatus());
            }

            hiddenDangerRecordVoList.add(hiddenDangerRecordVo);
        }

        ResponsePage<HiddenDangerRecordVo> hiddenDangerRecordVoResponsePage = new ResponsePage<HiddenDangerRecordVo>(hiddenDangerRecordVoList,Integer.parseInt(map.get("count")+""));
        return hiddenDangerRecordVoResponsePage;
    }

    /**
     * 隐患台账-隐患等级处理
     * @param dangerLevel 隐患等级
     * @return
     */
    private String getLevelName(String dangerLevel){
        if (dangerLevel==null){
            return "";
        }
        if (Dictionaries.DANGER_LEVEL_MANJOR.equals(dangerLevel)){
            return "重大隐患";
        }else if (Dictionaries.DANGER_LEVEL_A.equals(dangerLevel)){
            return "A级隐患";
        } if (Dictionaries.DANGER_LEVEL_B.equals(dangerLevel)){
            return "B级隐患";
        } if (Dictionaries.DANGER_LEVEL_C.equals(dangerLevel)){
            return "C级隐患";
        }
        return "";
    }

    private String getStatusName(String dangerStatus){
        if (dangerStatus==null){
            return "";
        }
        if ("0".equals(dangerStatus) || "100".equals(dangerStatus)){
            return "未整改";
        }else if ("1".equals(dangerStatus)){
            return "已整改";
        } if ("3".equals(dangerStatus)){
            return "整改中";
        }
        return "";
    }

    public HiddenDangerLevelNumVo getLevelNum(String mineCode){
        Dictionaries.setMineCode(mineCode);
        String time = getDate();
        HiddenDangerLevelNumVo hiddenDangerLevelNumVo = new HiddenDangerLevelNumVo();
        //隐患总数
        int dangerCount = hiddenDangerListDao.countAll(time,mineCode);
        hiddenDangerLevelNumVo.setDangerCount(dangerCount);

        //待五落实
        int rectified = hiddenDangerListDao.rectified(time,mineCode);
        hiddenDangerLevelNumVo.setImplement(rectified);

        //已闭合
        int closedLoop = hiddenDangerListDao.ClosedLoopCount(time,mineCode,Dictionaries.DANGER_STATE_CLOSEDLOOP);
        hiddenDangerLevelNumVo.setFinish(closedLoop);

        //待复查
        int pendingInspection = hiddenDangerListDao.pendingInspection(time,mineCode);
        hiddenDangerLevelNumVo.setPendingInspection(pendingInspection);


        return hiddenDangerLevelNumVo;
    }

    public HiddenDangerLevelNumVo getGaotouyaoLevelNum(String mineCode){
        Dictionaries.setMineCode(mineCode);
        String time = getDate();
        HiddenDangerLevelNumVo hiddenDangerLevelNumVo = new HiddenDangerLevelNumVo();
        //隐患总数
        int dangerCount = hiddenDangerListDao.gaoTouYaoCountAll(mineCode);
        hiddenDangerLevelNumVo.setDangerCount(dangerCount);

        //已整改
        int closedLoop = hiddenDangerListDao.gaoTouYaoClosedLoopCount(mineCode,Dictionaries.DANGER_STATE_CLOSEDLOOP);
        hiddenDangerLevelNumVo.setFinish(closedLoop);

        //未整改
        int rectified = hiddenDangerListDao.notRectified(mineCode);
        hiddenDangerLevelNumVo.setImplement(rectified);

        //整改中
        int pendingInspection = hiddenDangerListDao.inRectification(mineCode);
        hiddenDangerLevelNumVo.setPendingInspection(pendingInspection);

        return hiddenDangerLevelNumVo;
    }

    //隐患分析
    public Map<String,Object> dangerInfoList(String mineCode){
        Dictionaries.setMineCode(mineCode);
        Map<String,Object> map = new HashMap<>();
        String time  = getDate();
        map.put("majorList", hiddenDangerListDao.queryAll(time,mineCode, Dictionaries.DANGER_LEVEL_MANJOR));
        map.put("majorName", "重大隐患");
        map.put("normalAList", hiddenDangerListDao.queryAll(time,mineCode,Dictionaries.DANGER_LEVEL_A));
        map.put("normalAName", "一般A级隐患");
        map.put("normalBList", hiddenDangerListDao.queryAll(time,mineCode,Dictionaries.DANGER_LEVEL_B));
        map.put("normalBName", "一般B级隐患");
        map.put("normalCList", hiddenDangerListDao.queryAll(time,mineCode,Dictionaries.DANGER_LEVEL_C));
        map.put("normalCName", "一般C级隐患");

        return map;
    }

    //隐患分析
    public Map<String,Object> gaoTouYaoDangerInfoList(String mineCode){
        Dictionaries.setMineCode(mineCode);
        Map<String,Object> map = new HashMap<>();
        map.put("majorList", hiddenDangerListDao.gaoTouYaoQueryAll(mineCode, Dictionaries.DANGER_LEVEL_MANJOR));
        map.put("majorName", "重大隐患");
        map.put("normalAList", hiddenDangerListDao.gaoTouYaoQueryAll(mineCode,Dictionaries.DANGER_LEVEL_A));
        map.put("normalAName", "一般A级隐患");
        map.put("normalBList", hiddenDangerListDao.gaoTouYaoQueryAll(mineCode,Dictionaries.DANGER_LEVEL_B));
        map.put("normalBName", "一般B级隐患");
        map.put("normalCList", hiddenDangerListDao.gaoTouYaoQueryAll(mineCode,Dictionaries.DANGER_LEVEL_C));
        map.put("normalCName", "一般C级隐患");

        return map;
    }

    public Map<String, Object> sumDangerCheck(String mineCode) {

        Dictionaries.setMineCode(mineCode);
        String time = getDate();
        //隐患总数
        int dangerCount = hiddenDangerListDao.countAll(time,mineCode);


        SimpleDateFormat simpleDateFormatNew = new SimpleDateFormat("yyyy-MM-dd");
        String dataTime = simpleDateFormatNew.format(new Date());
        dataTime+=" 00:00:00";
        Timestamp timestampNew = Timestamp.valueOf(dataTime);
        //新增隐患
        List<HiddenDangerList> hiddenDangerLists = hiddenDangerListDao.findAllLikeDataTime(timestampNew,mineCode);

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Timestamp timestamp = Timestamp.valueOf(simpleDateFormat.format(new Date()));
        //待整改隐患
        int rectified = hiddenDangerListDao.rectified(time,mineCode);

        //所有超过期限的隐患
        int dealine = hiddenDangerListDao.countAllDeadline(time,mineCode);
        //已经验收的隐患总数
        int taccept = hiddenDangerListDao.ClosedLoopCount(time,mineCode,Dictionaries.DANGER_STATE_CLOSEDLOOP);
        Map<String,Object> map = new HashMap<>();
        map.put("totalHidden",dangerCount);
        map.put("newHidden",hiddenDangerLists.size());
        map.put("overdueHidden",dealine);
        map.put("notRectified",rectified);
        return map;
    }


    public void hiddenDangerLevel(TrinityLevel trinityLevel, String mineCode){
        Dictionaries.setMineCode(mineCode);
        String time = getDate();
        List<Map<String,Object>> list = hiddenDangerListDao.getLevelNum(time,mineCode);
        String major = Dictionaries.DANGER_LEVEL_MANJOR;
        String A = Dictionaries.DANGER_LEVEL_A;
        String B = Dictionaries.DANGER_LEVEL_B;
        String C = Dictionaries.DANGER_LEVEL_C;
        for(Map<String,Object> map : list){
            String dangerLevel = map.get("danger_level")+"";
            if (major.equals(dangerLevel)){
                trinityLevel.setMajorHiddenDanger(Integer.parseInt(map.get("count")+""));
                continue;
            }else if (A.equals(dangerLevel)){
                trinityLevel.setAHiddenDanger(Integer.parseInt(map.get("count")+""));
                continue;
            }else if (B.equals(dangerLevel)){
                trinityLevel.setBHiddenDanger(Integer.parseInt(map.get("count")+""));
                continue;
            }else if (C.equals(dangerLevel)){
                trinityLevel.setGenerallyHiddenDanger(Integer.parseInt(map.get("count")+""));
                continue;
            }

        }
    }


    public HiddenDangerCheckMonthVo getHiddenDangerCheckMonth(String mineCode){
        Dictionaries.setMineCode(mineCode);
        HiddenDangerCheckMonthVo hiddenDangerCheckMonthVo = new HiddenDangerCheckMonthVo();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.DATE,calendar.get(Calendar.DATE)-30);
        String time = simpleDateFormat.format(calendar.getTime());
        //新增的隐患
        List<Map<String,Object>> list = hiddenDangerListDao.countAllForDay(time,mineCode);
        Map<String,Integer> newHiddenMap = hiddenNumToMap(list);
        //已闭合隐患
        List<Map<String,Object>> closeList = hiddenDangerListDao.countAllForDayByClose(time,mineCode);
        Map<String,Integer> closeHiddenMap = hiddenNumToMap(closeList);

        List<String> xDate = new ArrayList<>();
        List<Integer> yNewHiddenDanger = new ArrayList<>();
        List<Integer> yFinish = new ArrayList<>();

        SimpleDateFormat simpleDateFormatDay = new SimpleDateFormat("MM-dd");
        for (int i=0;i<30;i++){
            if (i==0){
                calendar.set(Calendar.DATE,calendar.get(Calendar.DATE));
            }else {
                calendar.set(Calendar.DATE, calendar.get(Calendar.DATE) + 1);
            }
            String xDateString = simpleDateFormatDay.format(calendar.getTime());
            xDate.add(xDateString);
            String keyDateString = simpleDateFormat.format(calendar.getTime());
            yNewHiddenDanger.add(newHiddenMap.get(keyDateString)==null?0:newHiddenMap.get(keyDateString));
            yFinish.add(closeHiddenMap.get(keyDateString)==null?0:closeHiddenMap.get(keyDateString));
        }
        hiddenDangerCheckMonthVo.setXDate(xDate);
        hiddenDangerCheckMonthVo.setYNewHiddenDanger(yNewHiddenDanger);
        hiddenDangerCheckMonthVo.setYFinish(yFinish);
        calendar.clear();
        return hiddenDangerCheckMonthVo;
    }

    private Map<String,Integer> hiddenNumToMap(List<Map<String,Object>> list){
        Map<String,Integer> toMap = new HashMap<>();
        for (Map<String,Object> map : list){
            toMap.put(map.get("checkDate")+"",Integer.parseInt(map.get("count")+""));
        }
        return toMap;
    }

    public HiddenDangerCheckYearVo getHiddenDangerCheckYear(String mineCode){

        HiddenDangerCheckYearVo hiddenDangerCheckYearVo = new HiddenDangerCheckYearVo();

        Calendar calendar = Calendar.getInstance();
        Calendar calendarM = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR,calendarM.get(Calendar.YEAR));
        SimpleDateFormat day = new SimpleDateFormat("yyyy-MM-dd");
        //获取今年的新增隐患情况
        List<Map<String,Object>> newList = hiddenDangerListDao.countAllForMonth(day.format(calendar.getTime()),mineCode);
        Map<String,Integer> newListMap = hiddenNumToMap(newList);
        //获取上一年新增隐患情况
        calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR) - 1);
        List<Map<String,Object>> lastList = hiddenDangerListDao.countAllForMonth(day.format(calendar.getTime()),mineCode);
        Map<String,Integer> lastListMap = hiddenNumToMap(lastList);

        SimpleDateFormat month = new SimpleDateFormat("MM");
        SimpleDateFormat year = new SimpleDateFormat("yyyy-MM");
        List<String> xDate = new ArrayList<>();
        List<Integer> yNewHiddenDanger = new ArrayList<>();
        List<Integer> yLastYearHiddenDanger = new ArrayList<>();
        for (int i=0;i<12;i++){
            String lastYearString = year.format(calendar.getTime());

            calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR) + 1);
            String monthString = month.format(calendar.getTime());
            String yearString = year.format(calendar.getTime());

            xDate.add(monthString);
            yNewHiddenDanger.add(newListMap.get(yearString));
            yLastYearHiddenDanger.add(lastListMap.get(lastYearString));

            calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR) - 1);
            calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) + 1);

        }
        hiddenDangerCheckYearVo.setXDate(xDate);
        hiddenDangerCheckYearVo.setYLastYearHiddenDanger(yLastYearHiddenDanger);
        hiddenDangerCheckYearVo.setYNewHiddenDanger(yNewHiddenDanger);


        return hiddenDangerCheckYearVo;
    }

    public HiddenDangerCheckYearVo getGaoTouYaoHiddenDangerCheckYear(String mineCode){

        HiddenDangerCheckYearVo hiddenDangerCheckYearVo = new HiddenDangerCheckYearVo();

        Calendar calendar = Calendar.getInstance();
        Calendar calendarM = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR,calendarM.get(Calendar.YEAR));
        SimpleDateFormat day = new SimpleDateFormat("yyyy-MM-dd");
        //获取今年的新增隐患情况
        List<Map<String,Object>> newList = hiddenDangerListDao.gaoTouYaoCountAllForMonth(day.format(calendar.getTime()),mineCode);
        Map<String,Integer> newListMap = hiddenNumToMap(newList);
        //获取上一年新增隐患情况
        calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR) - 1);
        List<Map<String,Object>> lastList = hiddenDangerListDao.gaoTouYaoCountAllForMonth(day.format(calendar.getTime()),mineCode);
        Map<String,Integer> lastListMap = hiddenNumToMap(lastList);

        SimpleDateFormat month = new SimpleDateFormat("MM");
        SimpleDateFormat year = new SimpleDateFormat("yyyy-MM");
        List<String> xDate = new ArrayList<>();
        List<Integer> yNewHiddenDanger = new ArrayList<>();
        List<Integer> yLastYearHiddenDanger = new ArrayList<>();
        for (int i=0;i<12;i++){
            String lastYearString = year.format(calendar.getTime());

            calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR) + 1);
            String monthString = month.format(calendar.getTime());
            String yearString = year.format(calendar.getTime());

            xDate.add(monthString);
            yNewHiddenDanger.add(newListMap.get(yearString));
            yLastYearHiddenDanger.add(lastListMap.get(lastYearString));

            calendar.set(Calendar.YEAR, calendar.get(Calendar.YEAR) - 1);
            calendar.set(Calendar.MONTH, calendar.get(Calendar.MONTH) + 1);

        }
        hiddenDangerCheckYearVo.setXDate(xDate);
        hiddenDangerCheckYearVo.setYLastYearHiddenDanger(yLastYearHiddenDanger);
        hiddenDangerCheckYearVo.setYNewHiddenDanger(yNewHiddenDanger);


        return hiddenDangerCheckYearVo;
    }
   public List<DataStateVo> getMaxTime(String mineCode, String companyCode){
       String sql="SELECT mine_code as mineCode,max(create_time) as maxTime  FROM hidden_danger_list where 1=1 %s %s group by mine_code";
       if(StringUtils.isNotBlank(mineCode)){
           mineCode ="and mine_code='"+mineCode+"' ";
       }else{
           mineCode="";
       }
       if(StringUtils.isNotBlank(companyCode)&&StringUtils.isBlank(mineCode)){
           companyCode ="and company_code='"+companyCode+"' ";
       }else {
           companyCode="";
       }
       List<DataStateVo> query = locationJdbcTemplate.query(String.format(sql, mineCode, companyCode), new BeanPropertyRowMapper<>(DataStateVo.class));
       return query;
   }
}
